<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登入 - 平时成绩管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/login.css" media="all">
</head>
<body>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>平时成绩管理系统</h2>
            <p>大学计算机课程</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="LAY-user-login-username"></label>
                <input type="text" name="teacherId" id="LAY-user-login-username" lay-verify="required" placeholder="账号"
                       class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="LAY-user-login-password"></label>
                <input type="password" name="teacherPassword" id="LAY-user-login-password" lay-verify="required"
                       placeholder="密码" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                               for="LAY-user-login-vercode"></label>
                        <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required"
                               placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <div class="code">
                                <canvas id="canvas" width="100" height="43"></canvas>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
            </div>
        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2021 <a href="http://www.baidu.com/" target="_blank">dingwen.com</a></p>
        <p>
            <span>获取授权</span>
            <span>教师</span>
            <span>管理端</span>
        </p>
    </div>

</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function () {
        let $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        form.render();
        let showNum = [];
        /*验证码开始*/
        $(function () {
            draw(showNum);

            $("#canvas").on('click', function () {
                draw(showNum);
            })
        });

        function draw(showNum) {
            let canvas_width = $('#canvas').width();
            let canvas_height = $('#canvas').height();
            let canvas = document.getElementById("canvas");//获取到canvas的对象，演员
            let context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
            canvas.width = canvas_width;
            canvas.height = canvas_height;
            let sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            let aCode = sCode.split(",");
            let aLength = aCode.length;//获取到数组的长度

            for (let i = 0; i <= 3; i++) {
                let j = Math.floor(Math.random() * aLength);//获取到随机的索引值
                let deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
                let txt = aCode[j];//得到随机的一个内容
                showNum[i] = txt.toLowerCase();
                let x = 10 + i * 20;//文字在canvas上的x坐标
                let y = 20 + Math.random() * 8;//文字在canvas上的y坐标
                context.font = "bold 23px 微软雅黑";

                context.translate(x, y);
                context.rotate(deg);

                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);

                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (let i = 0; i <= 5; i++) { //验证码上显示线条
                context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (let i = 0; i <= 30; i++) { //验证码上显示小点
                context.strokeStyle = randomColor();
                context.beginPath();
                let x = Math.random() * canvas_width;
                let y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }

        function randomColor() {//得到随机的颜色值
            let r = Math.floor(Math.random() * 256);
            let g = Math.floor(Math.random() * 256);
            let b = Math.floor(Math.random() * 256);
            return "rgb(" + r + "," + g + "," + b + ")";
        }


        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            let codeInput = $("#LAY-user-login-vercode");
            let val = codeInput.val().toLowerCase();
            let num = showNum.join("");
            if (val === '') {
                layer.msg("请输入验证码");
            } else if (val === num) {
                codeInput.val('');
                draw(showNum);

                $.ajax({
                    url: layui.setter.baseUrl + '/teacher/login',
                    data: JSON.stringify(obj.field),
                    dataType: "json",
                    type: "post",
                    contentType: 'application/json',
                    success: function (res) {
                        if (res.code === 200) {
                          //存储用户信息
                          sessionStorage.setItem("user",JSON.stringify( res.data));
                            layui.data(setter.tableName, {
                                key: setter.request.tokenName
                                , value: obj.field.teacherId
                            });
                            layer.msg('登入成功', {
                                offset: '15px'
                                , icon: 1
                                , time: 1000
                            }, function () {
                                location.href = '../'; //后台主页
                            });
                        } else {
                            layer.msg("认证失败");
                        }

                    },
                    error: function (e) {
                        layer.msg("认证失败");
                    }
                });
            } else {
                layer.msg("验证码错误！请重新输入！");
                codeInput.val('');
                draw(showNum);
            }


        });

    });
</script>
</body>
</html>
